package com.pano.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.pano.dao.ImgDao;
import com.pano.dao.SceneDao;
import com.pano.dao.VpoiDao;
import com.pano.entity.Scene;
import com.pano.entity.Vpoi;
import com.pano.service.VpoiService;

@Service
public class VpoiServiceImpl implements VpoiService {

	@Autowired
	private VpoiDao vpoiDao;

	@Autowired
	private SceneDao sceneDao;

	@Autowired
	private ImgDao imgDao;

	@Override
	public List<Vpoi> findAll() {
		return vpoiDao.findAll(new Sort(Sort.Direction.DESC, "createTime"));
	}

	@Override
	@Transactional
	public void save(Vpoi vpoi) {
		vpoi.setCreateuserid("pano");
		vpoi.setCreateTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
		vpoiDao.save(vpoi);
	}

	@Override
	@Transactional
	public void delete(int id) {
		List<Scene> sceneList = sceneDao.findByVpoiid(id);
		for (int i = 0; i < sceneList.size(); i++) {
			imgDao.deleteByUrl(sceneList.get(i).getImgurl());
			sceneDao.delete(sceneList.get(i).getId());
		}
		vpoiDao.delete(id);
	}

	@Override
	@Transactional
	public void update(Vpoi vpoi) {
		Vpoi old = vpoiDao.findOne(vpoi.getId());
		old.setName(vpoi.getName());
		old.setSubdomain(vpoi.getSubdomain());
		old.setIntro(vpoi.getIntro());
		vpoiDao.save(old);
	}

}
